import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
// 1. 导入包
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  // 2. 配置为 vuex 的插件
  plugins: [createPersistedState()],
  state: {
    token: '',
    userinfo: {}
  },
  mutations: {
    // 更新token
    updateToken (state, payload) {
      state.token = payload
    },
    // 更新用户信息
    updateUserinfo (state, info) {
      state.userinfo = info
    }

  },
  actions: {
    async  initUserInfo (context) {
      const res = await axios.get('/my/userinfo'
      )
      // console.log(res.data.data)
      context.commit('updateUserinfo', res.data.data)
    }
  },
  modules: {
  }
})
